﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DreamBuilder.UI.UserControls.DataAccess.Services;
using DreamBuilder.Core.Container;
using System.Data;

namespace DreamBuilder.UI.UserControls
{
    public partial class AttachmentDownload1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string attachmentID = Request.QueryString["AttachmentID"];
            string AttachmentDBHelperString = Request.QueryString["AttachmentDBHelperString"];
            AttachmentDAL attachmentDAL = UContainer.Resolve<AttachmentDAL>();
            attachmentDAL.AttachmentDBHelperString = AttachmentDBHelperString;
            DataTable dt = attachmentDAL.GetAttachmentByID(attachmentID);

            string attachmentName = dt.Rows[0]["AttachmentName"].ToString();
            byte[] AttachmentContent = dt.Rows[0]["AttachmentContent"] as byte[];
            if (AttachmentContent == null)
            {
                Response.StatusCode = 404;
            }
            else
            {
                Response.AddHeader("Content-Disposition", "attachment; filename=\"" + HttpUtility.UrlEncode(attachmentName, System.Text.Encoding.UTF8) + "\"");
                Response.BinaryWrite(AttachmentContent);
                //modified by 陈杨 2011年4月14号17:21:25 修改上传文本文件增加后缀错误
                Response.Flush();
                Response.End();
            }
        }
    }
}
